*This do file extracts from the actual HRS (not RAND) data measures for health limitations , 
*** whether it is temporary (new), and whether it stops work altogether (new)

clear all
set more off
set maxvar 20000
global data 	"R:\SharedProjects\Shared2020-070\2016\extend_to_2020\JPE_Replication_dta"
global figures 	"R:\SharedProjects\Shared2020-070\2016\extend_to_2020\JPE_Replication_log"

cap log close
log using $figures\A_file,replace t

cd $data

** 1992
u  R:\Public\1992hrs\Final\RandFat\stata\h92f1b.dta, clear
keep v4001 v4006 v4102 v4103 v4020 v4106 v4126 v4130 hhidpn
rename v* V*
rename V4001 rhlthlm_direct1	//this is rhlthlm
replace rhlthlm_direct1 = 0 if rhlthlm_direct1 == 5

rename V4006 rhlth_temp1
replace rhlth_temp1 = 0 if rhlth_temp1 == 5

//can't work at all if 
rename V4102 rcantwork1
replace rcantwork1 = 0 if rcantwork1 == 5
replace rcantwork1 = 1 if V4103 == 1 & rcantwork1 != 1
replace rcantwork1 = 0 if rhlthlm_direct1 != . & rcantwork1 != 1 //everyone else is not stopped altogether
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct1 rhlth_temp1 rcantwork1 
label var rhlthlm_direct1 "W1 work limiting disability: HRS direct"
label var rhlth_temp1 "W1 disability is temporary"
label var rcantwork1 "W1 disability stops work altogether"

tempfile hrs1_clean
save `hrs1_clean'

** 1994
u  R:\Public\1994hrs\Final\RandFat\stata\h94f1a.dta, clear
keep w52* hhidpn
rename w* W*
rename W5200 rhlthlm_direct2
replace rhlthlm_direct2 = 0 if rhlthlm_direct2 == 5
replace rhlthlm_direct2 = . if rhlthlm_direct2 > 1

rename W5205 rhlth_temp2
replace rhlth_temp2 = . if rhlth_temp2 == 0			/*1=temporary, 5=not temporary*/
replace rhlth_temp2 = 0 if rhlth_temp2 == 5
replace rhlth_temp2 = . if rhlth_temp2 > 1

rename W5219 rcantwork2
replace rcantwork2 = . if rcantwork2 == 0							/*1=yes, keeps me from working, 5=no*/
replace rcantwork2 = 0 if rcantwork2 == 5 | rcantwork2 == 6
replace rcantwork2 = . if rcantwork2 > 1
replace rcantwork2 = 0 if rcantwork2 != 1 & rhlthlm_direct2 != .
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct2 rhlth_temp2 rcantwork2 
label var rhlthlm_direct2 "W2 work limiting disability: HRS direct"
label var rhlth_temp2 "W2 disability is temporary"
label var rcantwork2 "W2 disability stops work altogether"

merge 1:1 hhidpn using `hrs1_clean'
rename _merge _merge_1
tempfile hrs2_clean
save `hrs2_clean'

** 1996
u  R:\Public\1996hrs\Final\RandFat\stata\h96f4a.dta, clear
keep e35* hhidpn e1775 e1781 e1793 e1798 e1803 e1815 e1829 e1847

g hospcov3=e1775==1
g nurscov3=e1781==1
g surgcov3=e1793==1
g doctcov3=e1798==1
g dentcov3=e1803==1
g drugcov3=e1815==1
g homecov3=e1829==1
g helpcov3=e1847==1 

rename e* E*
rename E3507 rhlthlm_direct3
replace rhlthlm_direct3 = 0 if rhlthlm_direct3 == 5
replace rhlthlm_direct3 = . if rhlthlm_direct3 > 1

rename E3509 rhlth_temp3
replace rhlth_temp3 = 0 if rhlth_temp3 == 5
replace rhlth_temp3 = . if rhlth_temp3 > 1

rename E3522 rcantwork3
replace rcantwork3 = 0 if rcantwork3 == 5
replace rcantwork3 = . if rcantwork3 > 1
replace rcantwork3 = 0 if rcantwork3 != 1 & rhlthlm_direct3 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct3 rhlth_temp3 rcantwork3  hospcov-helpcov
label var rhlthlm_direct3 "W3 work limiting disability: HRS direct"
label var rhlth_temp3 "W3 disability is temporary"
label var rcantwork3 "W3 disability stops work altogether"


merge 1:1 hhidpn using `hrs2_clean'
rename _merge _merge_2
tempfile hrs3_clean
save `hrs3_clean'

**1998
u  R:\Public\1998hrs\Final\RandFat\stata\h98f2c.dta, clear
keep f40* hhidpn f42* f2298 f2304 f2332 f2334 f2336 f2346 f2359 f2377

g hospcov4=f2298==1
g nurscov4=f2304==1
g surgcov4=f2334==1
g doctcov4=f2332==1
g dentcov4=f2336==1
g drugcov4=f2346==1
g homecov4=f2359==1
g helpcov4=f2377==1 

rename f* F*

***In 1998 they break the disability module into two parts, old and new interviews 
replace F4017=F4201 if F4017==. & F4201!=.		/*new intvw*/
replace F4019=F4203 if F4019==. & F4203!=.		/*new intvw*/
replace F4032=F4238 if F4032==. & F4238!=.		/*new intvw - prevent from working, impairment started after began working*/
replace F4032=F4237 if F4032==. & F4237!=.		/*new intvw - prevent from working, impairment started before began working*/

rename F4017 rhlthlm_direct4
replace rhlthlm_direct4 = 0 if rhlthlm_direct4 == 5
replace rhlthlm_direct4 = . if rhlthlm_direct4 > 1

rename F4019 rhlth_temp4
replace rhlth_temp4 = 0 if rhlth_temp4 == 5
replace rhlth_temp4 = . if rhlth_temp4 > 1

rename F4032 rcantwork4
replace rcantwork4 = 0 if rcantwork4 == 5
replace rcantwork4 = . if rcantwork4 > 1
replace rcantwork4 = 0 if rcantwork4 != 1 & rhlthlm_direct4 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct4 rhlth_temp4 rcantwork4 hospcov-helpcov
label var rhlthlm_direct4 "W4 work limiting disability: HRS direct"
label var rhlth_temp4 "W4 disability is temporary"
label var rcantwork4 "W4 disability stops work altogether"


merge 1:1 hhidpn using `hrs3_clean'
rename _merge _merge_3
tempfile hrs4_clean
save `hrs4_clean'

**2000
u R:\Public\2000hrs\Final\RandFat\stata\h00f1d.dta, clear
keep g43* g45* hhidpn g2570 g2576 g2609 g2611 g2613 g2623 g2636 g2654

g hospcov5=g2570==1
g nurscov5=g2576==1
g surgcov5=g2609==1
g doctcov5=g2611==1
g dentcov5=g2613==1
g drugcov5=g2623==1
g homecov5=g2636==1
g helpcov5=g2654==1 

rename g* G*

***In 2000 they break the disability module into two parts, old and new interviews 
replace G4307=G4553 if G4307==. & G4553!=.		/*new intvw*/
replace G4309=G4555 if G4309==. & G4555!=.		/*new intvw*/
replace G4320=G4590 if G4320==. & G4590!=.		/*new intvw - prevent from working, impairment started after began working*/
replace G4320=G4589 if G4320==. & G4589!=.		/*new intvw - prevent from working, impairment started before began working*/

rename G4307 rhlthlm_direct5
replace rhlthlm_direct5 = 0 if rhlthlm_direct5 == 5
replace rhlthlm_direct5 = . if rhlthlm_direct5 > 1

rename G4309 rhlth_temp5
replace rhlth_temp5 = 0 if rhlth_temp5 == 5
replace rhlth_temp5 = . if rhlth_temp5 > 1

rename G4320 rcantwork5
replace rcantwork5 = 0 if rcantwork5 == 5
replace rcantwork5 = . if rcantwork5 > 1
replace rcantwork5 = 0 if rcantwork5 != 1 & rhlthlm_direct5 !=.

destring hhidpn, replace

keep hhidpn rhlthlm_direct5 rhlth_temp5 rcantwork5  hospcov-helpcov
label var rhlthlm_direct5 "W5 work limiting disability: HRS direct"
label var rhlth_temp5 "W5 disability is temporary"
label var rcantwork5 "W5 disability stops work altogether"


merge 1:1 hhidpn using `hrs4_clean'
rename _merge _merge_4
tempfile hrs5_clean
save `hrs5_clean'

**2002
u R:\Public\2002hrs\Final\RandFat\stata\h02f2c.dta, clear
keep hm00* hm014 hm5* hhidpn hn102 hn118 hn135 hn152 hn165 hn176 hn190 hn212  

g hospcov6=hn102==1
g nurscov6=hn118==1
g surgcov6=hn135==1
g doctcov6=hn152==1
g dentcov6=hn165==1
g drugcov6=hn176==1
g homecov6=hn190==1
g helpcov6=hn212==1 

rename hm* HM*

rename HM002 rhlthlm_direct6
replace rhlthlm_direct6 = 1 if HM502 == 1
replace rhlthlm_direct6 = 5 if HM502 == 5
replace rhlthlm_direct6 = 0 if rhlthlm_direct6 == 5
replace rhlthlm_direct6 = . if rhlthlm_direct6 > 1

rename HM004 rhlth_temp6
replace rhlth_temp6 = 1 if HM504 == 1
replace rhlth_temp6 = 5 if HM504 == 5
replace rhlth_temp6 = 0 if rhlth_temp6 == 5
replace rhlth_temp6 = . if rhlth_temp6 > 1

rename HM008 rcantwork6
replace rcantwork6 = 1 if HM527 == 1
replace rcantwork6 = 5 if HM527 == 5
replace rcantwork6 = 1 if HM528 == 1
replace rcantwork6 = 5 if HM528 == 5
replace rcantwork6 = 0 if rcantwork6 == 5
replace rcantwork6 = . if rcantwork6 > 1
replace rcantwork6 = 0 if rcantwork6 != 1 & rhlthlm_direct6 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct6 rhlth_temp6 rcantwork6  hospcov-helpcov
label var rhlthlm_direct6 "W6 work limiting disability: HRS direct"
label var rhlth_temp6 "W6 disability is temporary"
label var rcantwork6 "W6 disability stops work altogether"

merge 1:1 hhidpn using `hrs5_clean'
rename _merge _merge_5
tempfile hrs6_clean
save `hrs6_clean'



**2004
u  R:\Public\2004hrs\Final\RandFat\stata\h04f1c.dta, clear
keep jm00* jm014 jm5* hhidpn jn102 jn118 jn135 jn152 jn165 jn176 jn190 jn212

g hospcov7=jn102==1
g nurscov7=jn118==1
g surgcov7=jn135==1
g doctcov7=jn152==1
g dentcov7=jn165==1
g drugcov7=jn176==1
g homecov7=jn190==1
g helpcov7=jn212==1 

rename jm* JM*

rename JM002 rhlthlm_direct7
replace rhlthlm_direct7 = 1 if JM502 == 1
replace rhlthlm_direct7 = 5 if JM502 == 5
replace rhlthlm_direct7 = 0 if rhlthlm_direct7 == 5
replace rhlthlm_direct7= . if rhlthlm_direct7 > 1

rename JM004 rhlth_temp7
replace rhlth_temp7 = 1 if JM504 == 1
replace rhlth_temp7 = 5 if JM504 == 5
replace rhlth_temp7 = 0 if rhlth_temp7 == 5
replace rhlth_temp7 = . if rhlth_temp7 > 1

rename JM008 rcantwork7
replace rcantwork7 = 1 if JM527 == 1
replace rcantwork7 = 5 if JM527 == 5
replace rcantwork7 = 1 if JM528 == 1
replace rcantwork7 = 5 if JM528 == 5
replace rcantwork7 = 0 if rcantwork7 == 5
replace rcantwork7 = . if rcantwork7 > 1
replace rcantwork7 = 0 if rcantwork7 != 1 & rhlthlm_direct7 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0


destring hhidpn, replace

keep hhidpn rhlthlm_direct7 rhlth_temp7 rcantwork7  hospcov-helpcov
label var rhlthlm_direct7 "W7 work limiting disability: HRS direct"
label var rhlth_temp7 "W7 disability is temporary"
label var rcantwork7 "W7 disability stops work altogether"

merge 1:1 hhidpn using `hrs6_clean'
rename _merge _merge_6
tempfile hrs7_clean
save `hrs7_clean'

**2006
u R:\Public\2006hrs\Final\RandFat\stata\h06f3a.dta, clear
keep km00* km5* km014 hhidpn kn102 kn118 kn135 kn152 kn165 kn176 kn190 kn212

g hospcov8=kn102==1
g nurscov8=kn118==1
g surgcov8=kn135==1
g doctcov8=kn152==1
g dentcov8=kn165==1
g drugcov8=kn176==1
g homecov8=kn190==1
g helpcov8=kn212==1 

rename km* KM*

rename KM002 rhlthlm_direct8
replace rhlthlm_direct8 = 1 if KM502 == 1
replace rhlthlm_direct8 = 5 if KM502 == 5
replace rhlthlm_direct8 = 0 if rhlthlm_direct8 == 5
replace rhlthlm_direct8= . if rhlthlm_direct8 > 1

rename KM004 rhlth_temp8
replace rhlth_temp8 = 1 if KM504 == 1
replace rhlth_temp8 = 5 if KM504 == 5
replace rhlth_temp8 = 0 if rhlth_temp8 == 5
replace rhlth_temp8 = . if rhlth_temp8 > 1

rename KM008 rcantwork8
replace rcantwork8 = 1 if KM527 == 1
replace rcantwork8 = 5 if KM527 == 5
replace rcantwork8 = 1 if KM528 == 1
replace rcantwork8 = 5 if KM528 == 5
replace rcantwork8 = 0 if rcantwork8 == 5
replace rcantwork8 = . if rcantwork8 > 1
replace rcantwork8 = 0 if rcantwork8 != 1 & rhlthlm_direct8 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0


destring hhidpn, replace

keep hhidpn rhlthlm_direct8 rhlth_temp8 rcantwork8  hospcov-helpcov
label var rhlthlm_direct8 "W8 work limiting disability: HRS direct"
label var rhlth_temp8 "W8 disability is temporary"
label var rcantwork8 "W8 disability stops work altogether"


merge 1:1 hhidpn using `hrs7_clean'
rename _merge _merge_7

tempfile hrs8_clean
save `hrs8_clean'

**2008
u  R:\Public\2008hrs\Final\RandFat\stata\h08f3a.dta, clear
keep lm00* lm5* lm014 hhidpn ln102 ln118 ln135 ln152 ln165 ln176 ln190 ln212

g hospcov9=ln102==1
g nurscov9=ln118==1
g surgcov9=ln135==1
g doctcov9=ln152==1
g dentcov9=ln165==1
g drugcov9=ln176==1
g homecov9=ln190==1
g helpcov9=ln212==1 

rename lm* LM*

rename LM002 rhlthlm_direct9
replace rhlthlm_direct9 = 1 if LM502 == 1
replace rhlthlm_direct9 = 5 if LM502 == 5
replace rhlthlm_direct9 = 0 if rhlthlm_direct9 == 5
replace rhlthlm_direct9= . if rhlthlm_direct9 > 1

rename LM004 rhlth_temp9
replace rhlth_temp9 = 1 if LM504 == 1
replace rhlth_temp9 = 5 if LM504 == 5
replace rhlth_temp9 = 0 if rhlth_temp9 == 5
replace rhlth_temp9 = . if rhlth_temp9 > 1

rename LM008 rcantwork9
replace rcantwork9 = 1 if LM527 == 1
replace rcantwork9 = 5 if LM527 == 5
replace rcantwork9 = 1 if LM528 == 1
replace rcantwork9 = 5 if LM528 == 5
replace rcantwork9 = 0 if rcantwork9 == 5
replace rcantwork9 = . if rcantwork9 > 1
replace rcantwork9 = 0 if rcantwork9 != 1 & rhlthlm_direct9 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0


destring hhidpn, replace

keep hhidpn rhlthlm_direct9 rhlth_temp9 rcantwork9  hospcov-helpcov
label var rhlthlm_direct9 "W9 work limiting disability: HRS direct"
label var rhlth_temp9 "W9 disability is temporary"
label var rcantwork9 "W9 disability stops work altogether"


merge 1:1 hhidpn using `hrs8_clean'
rename _merge _merge_8


tempfile hrs9_clean
save `hrs9_clean'

**2010
u using R:\Public\2010hrs\Final\RandFat\stata\hd10f5f.dta, clear
keep mm00* mm5* mm014 hhidpn mn102 mn118 mn135 mn152 mn165 mn176 mn190 mn212 

g hospcov10=mn102==1
g nurscov10=mn118==1
g surgcov10=mn135==1
g doctcov10=mn152==1
g dentcov10=mn165==1
g drugcov10=mn176==1
g homecov10=mn190==1
g helpcov10=mn212==1 

rename mm* MM*

rename MM002 rhlthlm_direct10
replace rhlthlm_direct10 = 1 if MM502 == 1
replace rhlthlm_direct10 = 5 if MM502 == 5
replace rhlthlm_direct10 = 0 if rhlthlm_direct10 == 5
replace rhlthlm_direct10 = . if rhlthlm_direct10 > 1

rename MM004 rhlth_temp10
replace rhlth_temp10 = 1 if MM504 == 1
replace rhlth_temp10 = 5 if MM504 == 5
replace rhlth_temp10 = 0 if rhlth_temp10 == 5
replace rhlth_temp10 = . if rhlth_temp10 > 1

rename MM008 rcantwork10
replace rcantwork10 = 1 if MM527 == 1
replace rcantwork10 = 5 if MM527 == 5
replace rcantwork10 = 1 if MM528 == 1
replace rcantwork10 = 5 if MM528 == 5
replace rcantwork10 = 0 if rcantwork10 == 5
replace rcantwork10 = . if rcantwork10 > 1
replace rcantwork10 = 0 if rcantwork10 != 1 & rhlthlm_direct10 !=.


destring hhidpn, replace

keep hhidpn rhlthlm_direct10 rhlth_temp10 rcantwork10  hospcov-helpcov
label var rhlthlm_direct10 "W10 work limiting disability: HRS direct"
label var rhlth_temp10 "W10 disability is temporary"
label var rcantwork10 "W10 disability stops work altogether"


merge 1:1 hhidpn using `hrs9_clean'
rename _merge _merge_9


tempfile hrs10_clean
save `hrs10_clean'

**2012
u  R:\Public\2012hrs\Final\RandFat\stata\h12f3a.dta, clear
keep nm00* nm5* nm014 hhidpn nn102 nn118 nn135 nn152 nn165 nn176 nn190 nn212 

g hospcov11=nn102==1
g nurscov11=nn118==1
g surgcov11=nn135==1
g doctcov11=nn152==1
g dentcov11=nn165==1
g drugcov11=nn176==1
g homecov11=nn190==1
g helpcov11=nn212==1  

rename nm* NM*

rename NM002 rhlthlm_direct11
replace rhlthlm_direct11 = 1 if NM502 == 1
replace rhlthlm_direct11 = 5 if NM502 == 5
replace rhlthlm_direct11 = 0 if rhlthlm_direct11 == 5
replace rhlthlm_direct11 = . if rhlthlm_direct11 > 1

rename NM004 rhlth_temp11
replace rhlth_temp11 = 1 if NM504 == 1
replace rhlth_temp11 = 5 if NM504 == 5
replace rhlth_temp11 = 0 if rhlth_temp11 == 5
replace rhlth_temp11 = . if rhlth_temp11 > 1

rename NM008 rcantwork11
replace rcantwork11 = 1 if NM527 == 1
replace rcantwork11 = 5 if NM527 == 5
replace rcantwork11 = 1 if NM528 == 1
replace rcantwork11 = 5 if NM528 == 5
replace rcantwork11 = 0 if rcantwork11 == 5
replace rcantwork11 = . if rcantwork11 > 1
replace rcantwork11 = 0 if rcantwork11 != 1 & rhlthlm_direct11 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0


destring hhidpn, replace

keep hhidpn rhlthlm_direct11 rhlth_temp11 rcantwork11  hospcov-helpcov
label var rhlthlm_direct11 "W11 work limiting disability: HRS direct"
label var rhlth_temp11 "W11 disability is temporary"
label var rcantwork11 "W11 disability stops work altogether"

merge 1:1 hhidpn using `hrs10_clean'
rename _merge _merge_10


tempfile hrs11_clean
save `hrs11_clean'

**2014
u using R:\Public\2014hrs\Final\RandFat\stata\h14f2b.dta, clear
keep om00* om5* om014 hhidpn on434_1 on434_2 on434_3 on434_4 on434_5 on434_6 on434_7 on212

g hospcov12=on434_1==1
g nurscov12=on434_2==1
g surgcov12=on434_3==1
g doctcov12=on434_4==1
g dentcov12=on434_5==1
g drugcov12=on434_6==1
g homecov12=on434_7==1
g helpcov12=on212==1  

rename om* OM*

rename OM002 rhlthlm_direct12
replace rhlthlm_direct12 = 1 if OM502 == 1
replace rhlthlm_direct12 = 5 if OM502 == 5
replace rhlthlm_direct12 = 0 if rhlthlm_direct12 == 5
replace rhlthlm_direct12 = . if rhlthlm_direct12 > 1

rename OM004 rhlth_temp12
replace rhlth_temp12 = 1 if OM504 == 1
replace rhlth_temp12 = 5 if OM504 == 5
replace rhlth_temp12 = 0 if rhlth_temp12 == 5
replace rhlth_temp12 = . if rhlth_temp12 > 1

rename OM008 rcantwork12
replace rcantwork12 = 1 if OM527 == 1
replace rcantwork12 = 5 if OM527 == 5
replace rcantwork12 = 1 if OM528 == 1
replace rcantwork12 = 5 if OM528 == 5
replace rcantwork12 = 0 if rcantwork12 == 5
replace rcantwork12 = . if rcantwork12 > 1
replace rcantwork12 = 0 if rcantwork12 != 1 & rhlthlm_direct12 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct12 rhlth_temp12 rcantwork12  hospcov-helpcov
label var rhlthlm_direct12 	"W12 work limiting disability: HRS direct"
label var rhlth_temp12 		"W12 disability is temporary"
label var rcantwork12 		"W12 disability stops work altogether"

merge 1:1 hhidpn using `hrs11_clean'
rename _merge _merge_11


tempfile hrs12_clean
save `hrs12_clean'

**2016
u R:\Public\2016hrs\Final\RandFat\stata\h16f2c.dta, clear
keep pm00* pm5* pm014 hhidpn pn434_1 pn434_2 pn434_3 pn434_4 pn434_5 pn434_6 pn434_7 pn212

g hospcov13=pn434_1==1
g nurscov13=pn434_2==1
g surgcov13=pn434_3==1
g doctcov13=pn434_4==1
g dentcov13=pn434_5==1
g drugcov13=pn434_6==1
g homecov13=pn434_7==1
g helpcov13=pn212==1  

rename pm002 rhlthlm_direct13
replace rhlthlm_direct13 = 1 if pm502 == 1
replace rhlthlm_direct13 = 5 if pm502 == 5
replace rhlthlm_direct13 = 0 if rhlthlm_direct13 == 5
replace rhlthlm_direct13 = . if rhlthlm_direct13 > 1

rename pm004 rhlth_temp13
replace rhlth_temp13 = 1 if pm504 == 1
replace rhlth_temp13 = 5 if pm504 == 5
replace rhlth_temp13 = 0 if rhlth_temp13 == 5
replace rhlth_temp13 = . if rhlth_temp13 > 1

rename pm008 rcantwork13
replace rcantwork13 = 1 if pm527 == 1
replace rcantwork13 = 5 if pm527 == 5
replace rcantwork13 = 1 if pm528 == 1
replace rcantwork13 = 5 if pm528 == 5
replace rcantwork13 = 0 if rcantwork13 == 5
replace rcantwork13 = . if rcantwork13 > 1
replace rcantwork13 = 0 if rcantwork13 != 1 & rhlthlm_direct13 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0


destring hhidpn, replace

keep hhidpn rhlthlm_direct13 rhlth_temp13 rcantwork13  hospcov-helpcov
label var rhlthlm_direct13 	"W13 work limiting disability: HRS direct"
label var rhlth_temp13 		"W13 disability is temporary"
label var rcantwork13 		"W13 disability stops work altogether"

merge 1:1 hhidpn using `hrs12_clean'
rename _merge _merge_12


tempfile hrs13_clean
save `hrs13_clean'


**2018

use "R:\Public\2018hrs\Final\Core\stata\h18m2_r.dta",clear
egen hhidpn = concat(hhid pn)
rename *,lower
keep qm5* qm514 hhidpn
destring hhidpn, replace
sort hhidpn
save temp1,replace

use "R:\Public\2018hrs\Final\Core\stata\h18n_r.dta",clear
egen hhidpn = concat(hhid pn)
destring hhidpn, replace
rename *,lower
keep hhidpn qn434_1 qn434_2 qn434_3 qn434_4 qn434_5 qn434_6 qn434_7 qn212
destring hhidpn, replace
sort hhidpn
save temp2,replace

use "R:\Public\2018hrs\Final\Core\stata\h18m1_r.dta",clear
egen hhidpn = concat(hhid pn)
destring hhidpn, replace
rename *,lower
keep qm00* qm014 hhidpn
sort hhidpn
merge hhidpn using temp1
tab _merge
drop _merge
sort hhidpn
merge hhidpn using temp2
tab _merge

erase temp1.dta
erase temp2.dta

drop _merge

g hospcov14=qn434_1==1 
g nurscov14=qn434_2==1 
g surgcov14=qn434_3==1
g doctcov14=qn434_4==1
g dentcov14=qn434_5==1 
g drugcov14=qn434_6==1 
g homecov14=qn434_7==1 
g helpcov14=qn212==1  

rename qm002 rhlthlm_direct14
replace rhlthlm_direct14 = 1 if qm502 == 1
replace rhlthlm_direct14 = 5 if qm502 == 5
replace rhlthlm_direct14 = 0 if rhlthlm_direct14 == 5
replace rhlthlm_direct14 = . if rhlthlm_direct14 > 1 | rhlthlm_direct14 <0

rename qm004 rhlth_temp14
replace rhlth_temp14 = 1 if qm504 == 1
replace rhlth_temp14 = 5 if qm504 == 5
replace rhlth_temp14 = 0 if rhlth_temp14 == 5
replace rhlth_temp14 = . if rhlth_temp14 > 1 | rhlth_temp14 <0

rename qm008 rcantwork14
replace rcantwork14 = 1 if qm527 == 1
replace rcantwork14 = 5 if qm527 == 5
replace rcantwork14 = 1 if qm528 == 1
replace rcantwork14 = 5 if qm528 == 5
replace rcantwork14 = 0 if rcantwork14 == 5
replace rcantwork14 = . if rcantwork14 > 1 | rcantwork14 < 0
replace rcantwork14 = 0 if rcantwork14 != 1 & rhlthlm_direct14 !=.
replace rcantwork = 0 if rcantwork == 1 & rhlthlm_direct ==0


destring hhidpn, replace

keep hhidpn rhlthlm_direct14 rhlth_temp14 rcantwork14  hospcov-helpcov
label var rhlthlm_direct14 	"W14 work limiting disability: HRS direct"
label var rhlth_temp14 		"W14 disability is temporary"
label var rcantwork14 		"W14 disability stops work altogether"

merge 1:1 hhidpn using `hrs13_clean'
rename _merge _merge_13


tempfile hrs14_clean
save `hrs14_clean'


**2020

use "R:\Public\2020hrs\Core\stata\h20n_r.dta",clear
rename *,lower
egen hhidpn = concat(hhid pn)
destring hhidpn, replace
keep hhidpn rn434_1 rn434_2 rn434_3 rn434_4 rn434_5 rn434_6 rn434_7 rn212
destring hhidpn, replace
sort hhidpn
save temp2,replace

use "R:\Public\2020hrs\Core\stata\h20m2_r.dta",clear
rename *,lower
egen hhidpn = concat(hhid pn)

keep rm502 rm504 rm527 rm528 hhidpn
destring hhidpn, replace
sort hhidpn
save temp1,replace

use "R:\Public\2020hrs\Core\stata\h20m1_r.dta",clear
rename *,lower
egen hhidpn = concat(hhid pn)
destring hhidpn, replace
keep rm00* rm014 hhidpn
sort hhidpn
merge hhidpn using temp1
tab _merge
drop _merge
sort hhidpn
merge hhidpn using temp2
tab _merge

erase temp1.dta
erase temp2.dta

drop _merge

g hospcov15=rn434_1==1 
g nurscov15=rn434_2==1 
g surgcov15=rn434_3==1
g doctcov15=rn434_4==1
g dentcov15=rn434_5==1 
g drugcov15=rn434_6==1 
g homecov15=rn434_7==1 
g helpcov15=rn212==1  

rename rm002 rhlthlm_direct15
replace rhlthlm_direct15 = 1 if rm502 == 1
replace rhlthlm_direct15 = 5 if rm502 == 5
replace rhlthlm_direct15 = 0 if rhlthlm_direct15 == 5
replace rhlthlm_direct15 = . if rhlthlm_direct15 > 1 | rhlthlm_direct15 <0

rename rm004 rhlth_temp15
replace rhlth_temp15 = 1 if rm504 == 1
replace rhlth_temp15 = 5 if rm504 == 5
replace rhlth_temp15 = 0 if rhlth_temp15 == 5
replace rhlth_temp15 = . if rhlth_temp15 > 1 | rhlth_temp15 <0

rename rm008 rcantwork15
replace rcantwork15 = 1 if rm527 == 1
replace rcantwork15 = 5 if rm527 == 5
replace rcantwork15 = 1 if rm528 == 1
replace rcantwork15 = 5 if rm528 == 5
replace rcantwork15 = 0 if rcantwork15 == 5
replace rcantwork15 = . if rcantwork15 > 1 | rcantwork15 < 0
replace rcantwork15 = 0 if rcantwork15 != 1 & rhlthlm_direct15 !=.
replace rcantwork15 = 0 if rcantwork15 == 1 & rhlthlm_direct15 ==0

destring hhidpn, replace

keep hhidpn rhlthlm_direct15 rhlth_temp15 rcantwork15  hospcov-helpcov
label var rhlthlm_direct15 	"W15 work limiting disability: HRS direct"
label var rhlth_temp15 		"W15 disability is temporary"
label var rcantwork15 		"W15 disability stops work altogether"

merge 1:1 hhidpn using `hrs14_clean'
rename _merge _merge_14


cd $data
drop _merge*
save hrs_healthcond.dta, replace

log close
